<?php
// 引入数据库连接文件
if (!isset($con)) {
    die("Database connection variable is not set.");
}
?>
<div id="middle">
    <div id="left">
        <div id="up">
            <div class="news-title">热点新闻</div>
            <div id="news">
                <ul>
                    <?php
                    $newssql = "select * from news order by id desc limit 0,5";
                    $newsquery = mysqli_query($con, $newssql);
                    // 移除多余的 mysqli_fetch_array 调用
                    while ($newsrow = mysqli_fetch_array($newsquery, MYSQLI_ASSOC)) {
                    ?>
                        <li class="news"><a href="newslist.php?newsid=<?php echo $newsrow['id']; ?>"><?php echo substr($newsrow['title'], 0, 80); ?></a></li>
                    <?php
                    }
                    ?>
                </ul>
                <div class="more-link"><a href="newslist.php">更多新闻 >></a></div>
            </div>
        </div>
        <div id="down">
            <div class="news-title">个人信息</div>
            <ul class="profile-list">
                <?php
                $sql = "select * from qiye";
                $query = mysqli_query($con, $sql);
                $row = mysqli_fetch_array($query, MYSQLI_ASSOC);
                ?>
                <ul style="list-style-type: none; padding: 0;">
                    <li style="margin:2px;">电话：<?php echo $row['tel'] ?></li>
                    <li style="margin:2px;">传真：<?php echo $row['chuanzhen'] ?></li>
                    <li style="margin:2px;">邮箱：<?php echo $row['email'] ?></li>
                    <li style="margin:2px;">业务部负责人：<?php echo $row['yewuname'] ?></li>
                    <li style="margin:2px;">技术部负责人：<?php echo $row['jishuname'] ?></li>
                    <li style="margin:2px;">技术咨询电话：<?php echo $row['jishutel'] ?></li>

                </ul>
            </ul>
            <img src="images/wx1.png" class="wechat-qr" />
        </div>
    </div>
    <div id="right">

        <div class="pro">
            <div class="news-title">产品展示</div>
            <ul>
                <?php
                if (isset($_GET['proid']) && $_GET['proid']) {
                    $proid = $_GET['proid'];
                    $projssql = "select * from pro where id='" . $proid . "'";
                    $projsquery = mysqli_query($con, $projssql);
                    $projsrow = mysqli_fetch_array($projsquery, MYSQLI_ASSOC);
                    echo "<div class='product-detail'>";
                    echo "<div class='product-image'><img src=admin/uploadimg/" . $projsrow['propic'] . " alt=" . $projsrow['proname'] . " width=300 height=300></div>";
                    echo "<div class='product-description'>" . $projsrow['projieshao'] . "</div>";
                    echo "<div class='pagination' style='margin-top: 20px;'><a href='proshow.php' class='back-to-list'>返回列表</a></div>";
                    echo "</div>";
                } else {
                    // 分页参数处理
                    $per_page = 8; // 每页显示8个产品
                    $page = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
                    $offset = ($page - 1) * $per_page;
                    
                    // 获取总记录数
                    $count_sql = "SELECT COUNT(*) as total FROM pro";
                    $count_result = mysqli_query($con, $count_sql);
                    $total_rows = mysqli_fetch_assoc($count_result)['total'];
                    $total_pages = ceil($total_rows / $per_page);

                    // 分页查询
                    $prosql = "SELECT * FROM pro ORDER BY id DESC LIMIT $offset, $per_page";
                    $proquery = mysqli_query($con, $prosql);
                    
                    while ($prorow = mysqli_fetch_array($proquery, MYSQLI_ASSOC)) {
                        echo "<div class=protu><div class=protuku><a href=proshow.php?proid=" . $prorow['id'] . "><img src=admin/uploadimg/" . $prorow['propic'] . " width=100 height=100 alt=" . $prorow['proname'] . " /></a></div><div class=prozi><a href=proshow.php?proid=" . $prorow['id'] . " title=" . $prorow['proname'] . ">" . substr($prorow['proname'], 0, 18) . "</a></div></div>";
                    }
                    
                    // 添加分页导航
                    echo '<div class="pagination">';
                    if($page > 1) {
                        echo '<a href="?page='.($page-1).'">上一页</a>';
                    }
                    for($i=1; $i<=$total_pages; $i++) {
                        echo '<a href="?page='.$i.'"'.($i==$page ? ' class="current"' : '').'>'.$i.'</a>';
                    }
                    if($page < $total_pages) {
                        echo '<a href="?page='.($page+1).'">下一页</a>';
                    }
                    // 当用户在最后一页时显示返回首页按钮
                    if($page == $total_pages && $total_pages > 1) {
                        echo '<a href="?page=1" class="back-to-first">返回首页</a>';
                    }
                    echo '<span class="total-pages">共'.$total_pages.'页</span></div>';}
                
                ?>
            </ul>
        </div>
    </div>
</div>